"""
Case Type   : regexp_substr函数
Case Name   : pattern使用模式.（单个/多个）
Create At   : 2022/7/14
Owner       : @songjing20
Description :
    1.创建表插入数据
    2.pattern使用模式 .(单个）
    3.pattern使用模式 .(多个连续）
    4.pattern使用模式 .(间隔多个）
    5.清理环境
Expect      :
    1.成功
    2.抽取子串成功
    3.抽取子串成功
    4.成功，abcabca
    5.成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Sysfunction(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.commsh = CommonSH('dbuser')
        self.constant = Constant()
        self.tb_name = 't_ustore_regexp_substr_0018'
        self.db_name = 'db_ustore_regexp_substr_0018'
        create_db_res = self.commsh.execut_db_sql(
            f"create database {self.db_name} encoding 'utf8';")
        self.log.info(create_db_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_db_res,
                      f'预置utf8字符编码数据库失败')

    def test_regexp_substr(self):
        text = '-----step1:创建表插入数据;expect:成功-----'
        self.log.info(text)
        sql = f'''drop table if exists {self.tb_name} cascade;
        create table {self.tb_name}(c_id int, c_char char(55), 
        c_varchar varchar(55)) with (storage_type = ustore);
        insert into {self.tb_name} values(1, 'Fluffy', 'Fluffy');
        insert into {self.tb_name} values(2, 'Buffy', 'Buffy');
        insert into {self.tb_name} values(3, 'fluffy', 'fluffy');
        insert into {self.tb_name} values(4, 'buffy', 'buffy');
        insert into {self.tb_name} values(5, '桂林山水abc高山流水', '桂林山水abc高山流水');
        insert into {self.tb_name} values(6, 'aa abc zzzz', 'aa abc zzzz');
        insert into {self.tb_name} values
        (7, '我的的的的 abcabcabcabcabcabcabcabcabcabcabcabc', 
         '我的的的的 abcabcabcabcabcabcabcabcabcabcabcabc');
        insert into {self.tb_name} values
        (8, 'abcbvbnbefghijjkkkkkkk', 'abcbvbnbefghijjkkkkkkk123');
        insert into {self.tb_name} values(9, 'abc efg', 'hgj khln');
        insert into {self.tb_name} values(10, 'abc\efg', 'hgj(khln');
        insert into {self.tb_name} values
        (12, '\*+?|^$[](){{{{}}}}.', '\*+?|^$[](){{{{}}}}.');'''
        sql_res = self.commsh.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res,
                      f'建表失败:{text}')
        self.assertEqual(sql_res.count(self.constant.INSERT_SUCCESS_MSG), 11,
                         f'插入数据失败:{text}')

        text = '-----step2:pattern使用模式 .(单个）;expect:抽取子串成功-----'
        self.log.info(text)
        res = self.commsh.execut_db_sql(
            f"select c_id, regexp_substr(c_varchar, '.') from {self.tb_name} "
            f"group by c_id, regexp_substr(c_varchar, '.') order by 1;",
            dbname=self.db_name)
        self.log.info(res)
        self.assertIn("1 | F\n    2 | B\n    3 | f\n    4 | b\n    5 | 桂\n    "
                      "6 | a\n    7 | 我\n    8 | a\n    9 | h\n   10 | h\n   "
                      "12 | \\\n(11 rows)", res, f'执行失败:{text}')

        text = '-----step3:pattern使用模式 .(多个连续）;expect:抽取子串成功-----'
        self.log.info(text)
        res = self.commsh.execut_db_sql(
            f"select c_id, regexp_substr(c_varchar, '............bc') "
            f"from {self.tb_name} group by c_id, "
            f"regexp_substr(c_varchar, '............bc') order by 1;",
            dbname=self.db_name)
        self.log.info(res)
        msg = "1 | \n    2 | \n    3 | \n    4 | \n    5 | \n    6 | \n    " \
              "7 | {}\n    8 | \n    9 | \n   10 | \n   12 | \n(11 rows)"
        self.assertIn(msg.format('的的的的 abcabcabc'), res, f'执行失败:{text}')

        text = '-----step4:pattern使用模式 .(间隔多个）;expect:成功，abcabca-----'
        self.log.info(text)
        res = self.commsh.execut_db_sql(
            f"select c_id, regexp_substr(c_varchar, '.bc.bc.') "
            f"from {self.tb_name} "
            f"group by c_id, regexp_substr(c_varchar, '.bc.bc.') order by 1;",
            dbname=self.db_name)
        self.log.info(res)
        self.assertIn(msg.format('abcabca'), res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.log.info(text)
        drop_res = self.commsh.execut_db_sql(f"drop database {self.db_name};")
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res,
                      f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
